﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Microsoft.Win32;

namespace Hangu.WebClient.Controllers
{
    public class PlayController : Controller
    {
        //
        // GET: /Play/

        public ActionResult Index(string id)
        {
            string path = id;
            path = HanGu.Common.RijndaelSecurity.DecryptDES(path);
            if (path.IndexOf("http:") >= 0)
            {                
                return RedirectPermanent(path);
            }
            else
            {
                string fullpath = Server.MapPath(path);
                if (System.IO.File.Exists(fullpath))
                {

                    return File(fullpath, GetFileContentType(path));
                }
            }
            return View();
        }

        private  string GetFileContentType(string filename)
        {

            string[] array = filename.Split('.');

            string result = string.Empty;

            string suffix = "." + array[array.Length - 1];

            RegistryKey rg = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(suffix);

            object obj = rg.GetValue("Content Type");

            result = obj != null ? obj.ToString() : string.Empty;

            rg.Close();

            return result;

        }


        //
        // GET: /Play/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Play/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Play/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Play/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Play/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Play/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Play/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
